home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / DEMOS / PALTEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-14  |  4.2 KB  |  177 lines

  1. /* $Id: paltex.c,v 3.0 1998/02/14 18:42:29 brianp Exp $ */
  2.  
  3. /*
  4.  * Paletted texture demo.  Written by Brian Paul.  This file in public domain.
  5.  */
  6.  
  7. /*
  8.  * $Log: paltex.c,v $
  9.  * Revision 3.0  1998/02/14 18:42:29  brianp
  10.  * initial rev
  11.  *
  12.  */
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include <GL/glut.h>
  19.  
  20.  
  21. static float Rot = 0.0;
  22.  
  23.  
  24. static void Idle( void )
  25. {
  26.    Rot += 5.0;
  27.    glutPostRedisplay();
  28. }
  29.  
  30.  
  31. static void Display( void )
  32. {
  33.    glClear( GL_COLOR_BUFFER_BIT );
  34.  
  35.    glPushMatrix();
  36.    glRotatef(Rot, 0, 0, 1);
  37.  
  38.    glBegin(GL_POLYGON);
  39.    glTexCoord2f(0, 1);  glVertex2f(-1, -1);
  40.    glTexCoord2f(1, 1);  glVertex2f( 1, -1);
  41.    glTexCoord2f(1, 0);  glVertex2f( 1,  1);
  42.    glTexCoord2f(0, 0);  glVertex2f(-1,  1);
  43.    glEnd();
  44.  
  45.    glPopMatrix();
  46.  
  47.    glutSwapBuffers();
  48. }
  49.  
  50.  
  51. static void Reshape( int width, int height )
  52. {
  53.    glViewport( 0, 0, width, height );
  54.    glMatrixMode( GL_PROJECTION );
  55.    glLoadIdentity();
  56.    glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  57.    glMatrixMode( GL_MODELVIEW );
  58.    glLoadIdentity();
  59.    glTranslatef( 0.0, 0.0, -15.0 );
  60. }
  61.  
  62.  
  63. static void Key( unsigned char key, int x, int y )
  64. {
  65.    switch (key) {
  66.       case 27:
  67.          exit(0);
  68.          break;
  69.    }
  70.    glutPostRedisplay();
  71. }
  72.  
  73.  
  74. static void SpecialKey( int key, int x, int y )
  75. {
  76.    switch (key) {
  77.       case GLUT_KEY_UP:
  78.          break;
  79.       case GLUT_KEY_DOWN:
  80.          break;
  81.       case GLUT_KEY_LEFT:
  82.          break;
  83.       case GLUT_KEY_RIGHT:
  84.          break;
  85.    }
  86.    glutPostRedisplay();
  87. }
  88.  
  89.  
  90. static void Init( void )
  91. {
  92.    GLubyte texture[8][8] = {  /* PT = Paletted Texture! */
  93.       {  0,   0,   0,   0,   0,   0,   0,   0},
  94.       {  0, 100, 100, 100,   0, 180, 180, 180},
  95.       {  0, 100,   0, 100,   0,   0, 180,   0},
  96.       {  0, 100,   0, 100,   0,   0, 180,   0},
  97.       {  0, 100, 100, 100,   0,   0, 180,   0},
  98.       {  0, 100,   0,   0,   0,   0, 180,   0},
  99.       {  0, 100,   0,   0,   0,   0, 180,   0},
  100.       {  0, 100, 255,   0,   0,   0, 180, 250},
  101.    };
  102.  
  103.    GLubyte table[256][4];
  104.    int i;
  105.  
  106.    if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
  107.       printf("Sorry, GL_EXT_paletted_texture not supported\n");
  108.       exit(0);
  109.    }
  110.  
  111.    /* put some wacky colors into the texture palette */
  112.    for (i=0;i<256;i++) {
  113.       table[i][0] = i;
  114.       table[i][1] = 0;
  115.       table[i][2] = 127 + i / 2;
  116.       table[i][3] = 255;
  117.    }
  118.  
  119. #ifdef GL_EXT_paletted_texture
  120.  
  121. #if defined(GL_EXT_shared_texture_palette) && defined(SHARED_PALETTE)
  122.    printf("Using shared palette\n");
  123.    glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT,    /* target */
  124.                    GL_RGBA,          /* internal format */
  125.                    256,              /* table size */
  126.                    GL_RGBA,          /* table format */
  127.                    GL_UNSIGNED_BYTE, /* table type */
  128.                    table);           /* the color table */
  129.    glEnable(GL_SHARED_TEXTURE_PALETTE_EXT);
  130. #else
  131.    glColorTableEXT(GL_TEXTURE_2D,    /* target */
  132.                    GL_RGBA,          /* internal format */
  133.                    256,              /* table size */
  134.                    GL_RGBA,          /* table format */
  135.                    GL_UNSIGNED_BYTE, /* table type */
  136.                    table);           /* the color table */
  137. #endif
  138.  
  139.    glTexImage2D(GL_TEXTURE_2D,       /* target */
  140.                 0,                   /* level */
  141.                 GL_COLOR_INDEX8_EXT, /* internal format */
  142.                 8, 8,                /* width, height */
  143.                 0,                   /* border */
  144.                 GL_COLOR_INDEX,      /* texture format */
  145.                 GL_UNSIGNED_BYTE,    /* texture type */
  146.                 texture);            /* teh texture */
  147. #endif
  148.  
  149.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  150.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  151.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  152.    glEnable(GL_TEXTURE_2D);
  153. }
  154.  
  155.  
  156. int main( int argc, char *argv[] )
  157. {
  158.    glutInit( &argc, argv );
  159.    glutInitWindowPosition( 0, 0 );
  160.    glutInitWindowSize( 400, 400 );
  161.  
  162.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  163.  
  164.    glutCreateWindow(argv[0]);
  165.  
  166.    Init();
  167.  
  168.    glutReshapeFunc( Reshape );
  169.    glutKeyboardFunc( Key );
  170.    glutSpecialFunc( SpecialKey );
  171.    glutDisplayFunc( Display );
  172.    glutIdleFunc( Idle );
  173.  
  174.    glutMainLoop();
  175.    return 0;
  176. }
  177.